package com.tuongky.flagfan;

import java.util.Arrays;
import java.util.Random;

public class Initialization {
	
	// General
	final static int oo = 100100100;
	
	// SearchEngine
	final static int MAX_DEPTH = 10; // 50
	final static int MAX_BRANCH = 100;
	final static int WIN_VALUE = 100000;
	final static int DRAW_VALUE = 20;
	
	// Position
	final static int[] pieceTypes = 
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	 0,1,1,2,2,3,3,4,4,5,5,6,6,6,6,6,
	 0,1,1,2,2,3,3,4,4,5,5,6,6,6,6,6,};	
	
	final static int MAX_MOVE_NUM = 400;
	final static int NULL_MOVE = 0;
	final static int NONE_MOVE = -1;

	
	final static int NULL_MOVE_REDUCTION = 2;
	
	static boolean[] inBoard = new boolean[256];
	static boolean[] inPalace = new boolean[256];
	static int[] bitRankMask = new int[256];
	static int[] bitFileMask = new int[256];
	
	// TranspositionTable
	static long[][] zobristLockTable = new long[14][256];
	static long zobristLockPlayer;
	
	// Move Generation
	final static int kingMoveTab[]		= {-0x10, -0x01, +0x01, +0x10};
	final static int bishopMoveTab[]	= {-0x11, -0x0f, +0x0f, +0x11};
	final static int elephantMoveTab[]	= {-0x22, -0x1e, +0x1e, +0x22};
	final static int knightMoveTab[]	= {-0x21, -0x1f, -0x12, -0x0e, +0x0e, +0x12, +0x1f, +0x21};
	
	static int rookRankNoCapTab[][][] 	= new int[9][512][2];
	static int rookRankCapTab[][][]		= new int[9][512][2];
	static int cannonRankCapTab[][][]	= new int[9][512][2];

	static int rookFileNoCapTab[][][] 	= new int[10][1024][2];
	static int rookFileCapTab[][][]		= new int[10][1024][2];
	static int cannonFileCapTab[][][]	= new int[10][1024][2];
	
	static int rookRankNoCapMask[][]	= new int[9][512];
	static int rookRankCapMask[][]		= new int[9][512];
	static int cannonRankCapMask[][]	= new int[9][512];

	static int rookFileNoCapMask[][]	= new int[10][1024];
	static int rookFileCapMask[][]		= new int[10][1024];
	static int cannonFileCapMask[][]	= new int[10][1024];
	
	
	static int[][][] pawnMoves 		= new int[2][256][5];
	static int[][] bishopMoves 		= new int[256][5];
	static int[][] elephantMoves 	= new int[256][5];
	static int[][] knightMoves		= new int[256][10];
	static int[][] kingMoves		= new int[256][5];
	static int[][] elephantEyes		= new int[256][5];
	static int[][] horseLegs		= new int[256][10];
	
	final static int[] horseLegTab = 
{
                               0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,-16,  0,-16,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0, -1,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0, -1,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0, 16,  0, 16,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
   0,  0,  0,  0,  0,  0,  0
};	

	final static int[] legalMoveTab =
{
                       0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0
};

	// Evaluator
	
	final static int[][] posValues = {
		{
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  1,  1,  1,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0, 11, 15, 11,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
		},
		{
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0, 20,  0, 20,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0, 23,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0, 20,  0, 20,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
		},
		{
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0, 20,  0,  0,  0, 20,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0, 18,  0,  0,  0, 23,  0,  0,  0, 18,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0, 20,  0,  0,  0, 20,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
		},
		{
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,206,208,207,213,214,213,207,208,206,  0,  0,  0,  0,
		0,  0,  0,206,212,209,216,233,216,209,212,206,  0,  0,  0,  0,
		0,  0,  0,206,208,207,214,216,214,207,208,206,  0,  0,  0,  0,
		0,  0,  0,206,213,213,216,216,216,213,213,206,  0,  0,  0,  0,
		0,  0,  0,208,211,211,214,215,214,211,211,208,  0,  0,  0,  0,
		0,  0,  0,208,212,212,214,215,214,212,212,208,  0,  0,  0,  0,
		0,  0,  0,204,209,204,212,214,212,204,209,204,  0,  0,  0,  0,
		0,  0,  0,198,208,204,212,212,212,204,208,198,  0,  0,  0,  0,
		0,  0,  0,200,208,206,212,200,212,206,208,200,  0,  0,  0,  0,
		0,  0,  0,194,206,204,212,200,212,204,206,194,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
		},
		{
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,100,100, 96, 91, 90, 91, 96,100,100,  0,  0,  0,  0,
		0,  0,  0, 98, 98, 96, 92, 89, 92, 96, 98, 98,  0,  0,  0,  0,
		0,  0,  0, 97, 97, 96, 91, 92, 91, 96, 97, 97,  0,  0,  0,  0,
		0,  0,  0, 96, 99, 99, 98,100, 98, 99, 99, 96,  0,  0,  0,  0,
		0,  0,  0, 96, 96, 96, 96,100, 96, 96, 96, 96,  0,  0,  0,  0,
		0,  0,  0, 95, 96, 99, 96,100, 96, 99, 96, 95,  0,  0,  0,  0,
		0,  0,  0, 96, 96, 96, 96, 96, 96, 96, 96, 96,  0,  0,  0,  0,
		0,  0,  0, 97, 96,100, 99,101, 99,100, 96, 97,  0,  0,  0,  0,
		0,  0,  0, 96, 97, 98, 98, 98, 98, 98, 97, 96,  0,  0,  0,  0,
		0,  0,  0, 96, 96, 97, 99, 99, 99, 97, 96, 96,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
		},
		{
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0, 90, 90, 90, 96, 90, 96, 90, 90, 90,  0,  0,  0,  0,
		0,  0,  0, 90, 96,103, 97, 94, 97,103, 96, 90,  0,  0,  0,  0,
		0,  0,  0, 92, 98, 99,103, 99,103, 99, 98, 92,  0,  0,  0,  0,
		0,  0,  0, 93,108,100,107,100,107,100,108, 93,  0,  0,  0,  0,
		0,  0,  0, 90,100, 99,103,104,103, 99,100, 90,  0,  0,  0,  0,
		0,  0,  0, 90, 98,101,102,103,102,101, 98, 90,  0,  0,  0,  0,
		0,  0,  0, 92, 94, 98, 95, 98, 95, 98, 94, 92,  0,  0,  0,  0,
		0,  0,  0, 93, 92, 94, 95, 92, 95, 94, 92, 93,  0,  0,  0,  0,
		0,  0,  0, 85, 90, 92, 93, 78, 93, 92, 90, 85,  0,  0,  0,  0,
		0,  0,  0, 88, 85, 90, 88, 90, 88, 90, 85, 88,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
		},
		{
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  9,  9,  9, 11, 13, 11,  9,  9,  9,  0,  0,  0,  0,
		0,  0,  0, 29, 39, 59, 74, 79, 74, 59, 39, 29,  0,  0,  0,  0,
		0,  0,  0, 29, 39, 54, 64, 64, 64, 54, 39, 29,  0,  0,  0,  0,
		0,  0,  0, 29, 36, 39, 49, 51, 49, 39, 36, 29,  0,  0,  0,  0,
		0,  0,  0, 19, 27, 31, 44, 49, 44, 31, 27, 19,  0,  0,  0,  0,
		0,  0,  0,  7,  0, 13,  0, 16,  0, 13,  0,  7,  0,  0,  0,  0,
		0,  0,  0,  7,  0,  7,  0, 15,  0,  7,  0,  7,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		},
	};
	
	// Searcher
	
	void initPosition() {
		Arrays.fill(inBoard, false);
		for (int i=3; i<13; i++)
		for (int j=3; j<12; j++) inBoard[(i<<4)+j] = true;
		
		Arrays.fill(inPalace, false);		
		for (int j=6; j<=8; j++) {
			for (int i=3; i<=5; i++) inPalace[(i<<4)+j] = true;
			for (int i=10; i<=12; i++) inPalace[(i<<4)+j] = true;
		}
		
		Arrays.fill(bitRankMask, 0);
		Arrays.fill(bitFileMask, 0);
		for (int sq=0; sq<256; sq++)
		if (inBoard[sq]) {
			bitRankMask[sq] = 1 << (sq&0xf);
			bitFileMask[sq] = 1 << (sq>>4);
		}
	}
	
	void initTranspositionTable() {
		Random rnd = new Random(123456789);
		for (int i=0; i<14; i++)
		for (int j=0; j<256; j++) zobristLockTable[i][j] = rnd.nextLong();
		zobristLockPlayer = rnd.nextLong();
	}
	
	void initMoveGeneration() {
		// King & Bishop
		
		int src, dst, ind;
		
		for (src=0; src<256; src++)
		if (inPalace[src]) {
			ind = 0;
			for (int i=0; i<4; i++) {
				dst = src + kingMoveTab[i];
				if (inPalace[dst]) kingMoves[src][ind++] = dst;
			}
			kingMoves[src][ind] = 0;
			
			ind = 0;
			for (int i=0; i<4; i++) {
				dst = src + bishopMoveTab[i];
				if (inPalace[dst]) bishopMoves[src][ind++] = dst;
			}
			bishopMoves[src][ind] = 0;
		} else kingMoves[src][0] = bishopMoves[src][0] = 0;
		
		// Elephant, Knight & Pawn
		
		for (src=0; src<256; src++)
		if (inBoard[src]) {
			ind = 0;
			for (int i=0; i<4; i++) {
				dst = src + elephantMoveTab[i];
				if (inBoard[dst]&&((src^dst)&0x80)==0) {
					elephantMoves[src][ind] = dst;
					elephantEyes[src][ind] = (src+dst) >> 1;
					ind++;
				}
			}
			elephantMoves[src][ind] = 0;
			
			ind = 0;
			for (int i=0; i<8; i++) {
				dst = src + knightMoveTab[i];
				if (inBoard[dst]) {
					knightMoves[src][ind] = dst;
					horseLegs[src][ind] = src + horseLegTab[dst-src+256];
					ind++;
				}
			}
			knightMoves[src][ind] = 0;
			
			for (int i=0; i<2; i++) {
				ind = 0;
				if (i==0) dst = src - 16; else dst = src + 16;
				if (inBoard[dst]) pawnMoves[i][src][ind++] = dst;
				
				if (i==0?(src&0x80)==0 : (src&0x80)!=0) {
					for (int j=-1; j<=1; j+=2) {
						dst = src + j;
						if (inBoard[dst]) pawnMoves[i][src][ind++] = dst;
					}
				}
				pawnMoves[i][src][ind] = 0;
			}
		}
		
		// Rook, Cannon
		
		// RANK
		for (int i=0; i<9; i++)
		for (int j=0; j<512; j++) {
			rookRankNoCapTab[i][j][0] = rookRankNoCapTab[i][j][1] = i+3;
			rookRankCapTab[i][j][0] = rookRankCapTab[i][j][1] = i+3;
			cannonRankCapTab[i][j][0] = cannonRankCapTab[i][j][1] = i+3;
			rookRankNoCapMask[i][j] = rookRankCapMask[i][j] = cannonRankCapMask[i][j] = 0;
			
			int k;

			for (k=i+1; k<9; k++) {
				if ((j>>k&1)!=0) {
					rookRankCapTab[i][j][0] = k+3;
					rookRankCapMask[i][j] |= 1<<(k+3);
					break;
				}
				rookRankNoCapTab[i][j][0] = k+3;
				rookRankNoCapMask[i][j] |= 1<<(k+3);
			}
			
			for (k++; k<9; k++)
			if ((j>>k&1)!=0) {
				cannonRankCapTab[i][j][0] = k+3;
				cannonRankCapMask[i][j] |= 1<<(k+3);
				break;
			}

			for (k=i-1; k>=0; k--) {
				if ((j>>k&1)!=0) {
					rookRankCapTab[i][j][1] = k+3;
					rookRankCapMask[i][j] |= 1<<(k+3);
					break;
				}
				rookRankNoCapTab[i][j][1] = k+3;
				rookRankNoCapMask[i][j] |= 1<<(k+3);
			}
			
			for (k--; k>=0; k--)
			if ((j>>k&1)!=0) {
				cannonRankCapTab[i][j][1] = k+3;
				cannonRankCapMask[i][j] |= 1<<(k+3);
				break;
			}
			
		}

		// FILE
		for (int i=0; i<10; i++)
		for (int j=0; j<1024; j++) {
			rookFileNoCapTab[i][j][0] = rookFileNoCapTab[i][j][1] = i+3;
			rookFileCapTab[i][j][0] = rookFileCapTab[i][j][1] = i+3;
			cannonFileCapTab[i][j][0] = cannonFileCapTab[i][j][1] = i+3;
			rookFileNoCapMask[i][j] = rookFileCapMask[i][j] = cannonFileCapMask[i][j] = 0;
			
			int k;

			for (k=i+1; k<10; k++) {
				if ((j>>k&1)!=0) {
					rookFileCapTab[i][j][0] = k+3;
					rookFileCapMask[i][j] |= 1<<(k+3);
					break;
				}
				rookFileNoCapTab[i][j][0] = k+3;
				rookFileNoCapMask[i][j] |= 1<<(k+3);
			}
			
			for (k++; k<10; k++)
			if ((j>>k&1)!=0) {
				cannonFileCapTab[i][j][0] = k+3;
				cannonFileCapMask[i][j] |= 1<<(k+3);
				break;
			}

			for (k=i-1; k>=0; k--) {
				if ((j>>k&1)!=0) {
					rookFileCapTab[i][j][1] = k+3;
					rookFileCapMask[i][j] |= 1<<(k+3);
					break;
				}
				rookFileNoCapTab[i][j][1] = k+3;
				rookFileNoCapMask[i][j] |= 1<<(k+3);
			}
			
			for (k--; k>=0; k--)
			if ((j>>k&1)!=0) {
				cannonFileCapTab[i][j][1] = k+3;
				cannonFileCapMask[i][j] |= 1<<(k+3);
				break;
			}
			
		}

	}
	
	public Initialization() {
		initPosition();
		initTranspositionTable();
		initMoveGeneration();
	}
	
}
